// Fill out your copyright notice in the Description page of Project Settings.


#include "MyQuadtreeNodeManager.h"
#include "MyQuadtreeNode.h"
#include "MyFPoi.h"


// Sets default values
AMyQuadtreeNodeManager::AMyQuadtreeNodeManager()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
}

TArray<AMyFPoi*> AMyQuadtreeNodeManager::GenerateRandomPois(int num, TSubclassOf<AMyFPoi> PoiClass)
{
	TArray<AMyFPoi*> Pois;

	UWorld* world = GetWorld();
	if (!world)
	{
		UE_LOG(LogTemp, Warning, TEXT("world is null"));
		return Pois;
	}

	for (int i = 0; i < num; ++i)
	{
		// 随机生成位置
		FVector Location = FVector(FMath::RandRange(startPoint.X, startPoint.X+width),
			FMath::RandRange(startPoint.Y, startPoint.Y+height),
			zHeight);

		// 生成FPoi对象
		AMyFPoi* NewPoi = world->SpawnActor<AMyFPoi>(PoiClass, Location, FRotator::ZeroRotator);

		if (NewPoi)
		{
			// 生成随机名称和类型
			NewPoi->Name = FString::Printf(TEXT("Poi_%d"), i);
			NewPoi->Type = (i % 3 == 0) ? TEXT("TypeA") : TEXT("TypeB");
			NewPoi->setMaterial(i%3==0);

			Pois.Add(NewPoi);
			if(NewPoi->Type=="TypeA")
			{
				QuadtreeNodeA->insert_point_(FVector2D(NewPoi->GetActorLocation().X,NewPoi->GetActorLocation().Y));
			}
			else
			{
				QuadtreeNodeB->insert_point_(FVector2D(NewPoi->GetActorLocation().X,NewPoi->GetActorLocation().Y));
			}
		}
	}
	return Pois;
}

void AMyQuadtreeNodeManager::Init()
{
	QuadtreeNodeA->Init(this->startPoint,this->width,this->height);
	QuadtreeNodeB->Init(this->startPoint,this->width,this->height);
}


// Called when the game starts or when spawned
void AMyQuadtreeNodeManager::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyQuadtreeNodeManager::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

